const Webpack = require('webpack');
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const Moment = require('moment');

const gitRevisionPlugin = new GitRevisionPlugin();

module.exports = {
    devServer: {
        open: true,
        port: 5080,
        overlay: {
            warnings: true,
            errors: true
        }
    },

    css: {
        loaderOptions: {
            sass: {
                prependData: `@import "./src/assets/style/basic/mixins.scss"; @import "./src/assets/style/basic/variables.scss";`
            }
        }
    },

    configureWebpack: config => {
        config.entry = './src/main.js';
        config.devtool = process.env.NODE_ENV === 'production' ? false : 'source-map';

        const pluginsProd = [
                new Webpack.BannerPlugin(`打包时间: ${Moment().format('YYYY-MM-DD,hh:mm:ss')}`),
                new HardSourceWebpackPlugin()
            ],
            pluginsDev = [new CaseSensitivePathsPlugin()],
            pluginsBase = [
                new Webpack.ContextReplacementPlugin(/moment[\\/]locale$/, /^\.\/(zh-cn)$/),
                new Webpack.DefinePlugin({
                    VERSION: JSON.stringify(gitRevisionPlugin.version()),
                    COMMITHASH: JSON.stringify(gitRevisionPlugin.commithash()),
                    BRANCH: JSON.stringify(gitRevisionPlugin.branch()),
                    BUILDDATE: JSON.stringify(new Date())
                })
            ];

        if (process.env.NODE_ENV === 'production') {
            config.plugins = [...config.plugins, ...pluginsBase, ...pluginsProd];

            config.optimization = {
                minimizer: [
                    new UglifyJsPlugin({
                        parallel: true,
                        test: /\.js(\?.*)?$/i,
                        uglifyOptions: {
                            compress: {
                                pure_funcs: ['console.log']
                            },
                            warnings: false,
                            output: {
                                keep_quoted_props: true
                            }
                        }
                    }),
                    new OptimizeCSSPlugin({
                        assetNameRegExp: /\.css$/g,
                        cssProcessorOptions: {
                            safe: true,
                            autoprefixer: false,
                            discardComments: {
                                removeAll: true
                            }
                        },
                        cssProcessor: require('cssnano'),
                        cssProcessorPluginOptions: {
                            preset: ['default', { discardComments: { removeAll: true } }]
                        },
                        canPrint: false
                    })
                ],
                splitChunks: {
                    chunks: 'all',
                    minSize: 20000,
                    minChunks: 2,
                    maxAsyncRequests: 5,
                    maxInitialRequests: 3,
                    name: true,
                    automaticNameDelimiter: '-',
                    cacheGroups: {
                        libs: {
                            chunks: 'initial',
                            test: /[\\/]node_modules[\\/]/,
                            name: 'chunk-libs',
                            priority: 10,
                            enforce: true,
                            reuseExistingChunk: true
                        },
                        commons: {
                            test: /common\/|components\/|utils\//,
                            name: 'chunk-commons',
                            minChunks: 3,
                            priority: 5,
                            enforce: true,
                            reuseExistingChunk: true
                        },
                        vantUI: {
                            name: 'chunk-element',
                            priority: 20,
                            test: /[\\/]node_modules[\\/]_?element-ui(.*)/
                        }
                    }
                },
                runtimeChunk: {
                    name: 'manifest'
                }
            };
        } else {
            config.plugins = [...config.plugins, ...pluginsBase, ...pluginsDev];
        }
    },

    chainWebpack: config => {
        config.module
            .rule('images')
            .use('url-loader')
            .loader('url-loader')
            .tap(options => Object.assign(options, { limit: 10240 }));
    },

    publicPath: process.env.NODE_ENV === 'production' ? './' : '/'
};
